<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>API测试页面</title>
    <style>
        body { font-family: Arial, sans-serif; margin: 20px; }
        button { padding: 10px 20px; margin: 10px; background: #007bff; color: white; border: none; border-radius: 5px; cursor: pointer; }
        button:hover { background: #0056b3; }
        #result { margin-top: 20px; padding: 10px; background: #f8f9fa; border: 1px solid #ddd; border-radius: 5px; }
        .error { background: #f8d7da; border-color: #f5c6cb; color: #721c24; }
        .success { background: #d4edda; border-color: #c3e6cb; color: #155724; }
    </style>
</head>
<body>
    <h1>API连接测试</h1>
    
    <button onclick="testConnection()">测试后端连接</button>
    <button onclick="testLatestData()">获取最新数据</button>
    <button onclick="testStatus()">获取系统状态</button>
    
    <div id="result">点击按钮开始测试...</div>
    
    <script>
        const API_BASE_URL = 'http://localhost:8081/api/sensor';
        
        function updateResult(message, isError = false) {
            const resultDiv = document.getElementById('result');
            resultDiv.textContent = message;
            resultDiv.className = isError ? 'error' : 'success';
        }
        
        async function testConnection() {
            try {
                updateResult('正在测试连接...', false);
                const response = await fetch(API_BASE_URL + '/health');
                if (response.ok) {
                    const data = await response.json();
                    updateResult('连接成功！响应: ' + JSON.stringify(data, null, 2), false);
                } else {
                    updateResult('连接失败！HTTP状态: ' + response.status, true);
                }
            } catch (error) {
                updateResult('连接错误: ' + error.message, true);
            }
        }
        
        async function testLatestData() {
            try {
                updateResult('正在获取最新数据...', false);
                const response = await fetch(API_BASE_URL + '/latest');
                if (response.ok) {
                    const data = await response.json();
                    updateResult('数据获取成功！\n' + JSON.stringify(data, null, 2), false);
                } else {
                    updateResult('数据获取失败！HTTP状态: ' + response.status, true);
                }
            } catch (error) {
                updateResult('数据获取错误: ' + error.message, true);
            }
        }
        
        async function testStatus() {
            try {
                updateResult('正在获取系统状态...', false);
                const response = await fetch(API_BASE_URL + '/status');
                if (response.ok) {
                    const data = await response.json();
                    updateResult('状态获取成功！\n' + JSON.stringify(data, null, 2), false);
                } else {
                    updateResult('状态获取失败！HTTP状态: ' + response.status, true);
                }
            } catch (error) {
                updateResult('状态获取错误: ' + error.message, true);
            }
        }
    </script>
</body>
</html>